package com.example.sqlitetest.Activity;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;


import butterknife.ButterKnife;

public class AddActivity extends AppCompatActivity {


    EditText addText1,addText2,addText3,addText4,addText5,addText6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);
        Context context;

        //初始化数据库对象
        MyDataBaseHelper helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对
        final SQLiteDatabase db = helper.getWritableDatabase();//初始化SQLiteDatabase对象


        ImageButton  backlist = findViewById(R.id.backList);
        Button  submitBtn  = findViewById(R.id.submitBtn);

        addText1 = findViewById(R.id.addText1);
        addText2 = findViewById(R.id.addText2);
        addText3 = findViewById(R.id.addText3);
        addText4 = findViewById(R.id.addText4);
        addText5 = findViewById(R.id.addText5);
        addText6 =  findViewById(R.id.addText6);

        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(AddActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String name = addText1.getText().toString();
                final String className  = addText2.getText().toString();
                final String studentNo  = addText3.getText().toString();
                final String chinese  = addText4.getText().toString();
                final String math  = addText5.getText().toString();
                final String english = addText6.getText().toString();
                ContentValues values = new ContentValues();
                values.put("name",name);
                values.put("className",className);
                values.put("studentNo",studentNo);
                values.put("chinese",chinese);
                values.put("math",math);
                values.put("english",english);
                db.insert("Student", null, values);
                values.clear();
                Toast.makeText(AddActivity.this,"添加数据成功", Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent(AddActivity.this,MainActivity.class);
                startActivity(intent2);
                finish();
            }
        });
    }


}
